<?php
namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\Hash;
use function Matrix\add;    //引入hash

class LisenceApplyRepository
{

    /**
     * 获取资质审核列表
     * @param  $param
     * @param  $page
     * @param  $perPage
     * @return bool
     */
    public function applyList($param,$page=1,$perPage=20)
    {
        $list = app('model_lisence_apply')
            ->with('lisence:lisence_id,lisence_name')
            ->with('user_data:user_id,user_code,nickname')
            ->where('is_show',2);
        if (isset($param['lisence_id'])){
            $list=$list->where('lisence_id',$param['lisence_id']);
        }
        if (isset($param['status'])){
            $list=$list->where('status',$param['status']);
        }
        if (isset($param['user_code'])){
            $user_code = $param['user_code'];
            $list =$list->whereHas('user_data',function ($query) use ($user_code){
                $query->where('user_code','like',"%{$user_code}%");
            });
        }
        $list=$list->orderBy('status','asc')
            ->orderBy('add_time','asc')
            ->paginate($perPage, ['*'], '', $page);
        return $list;
    }


    /**
     * 获取资质审核列表
     * @param  $param
     * @param  $page
     * @param  $perPage
     * @return bool
     */
    public function applyShowList($param,$page=1,$perPage=20)
    {
        $list = app('model_lisence_apply')
            ->with('lisence:lisence_id,lisence_name')
            ->with('user_data:user_id,user_code,nickname')
            ->where('is_show',1);
        if (isset($param['lisence_id'])){
            $list=$list->where('lisence_id',$param['lisence_id']);
        }
        if (isset($param['status'])){
            $list=$list->where('status',$param['status']);
        }
        if (isset($param['user_code'])){
            $user_code = $param['user_code'];
            $list =$list->whereHas('user_data',function ($query) use ($user_code){
                $query->where('user_code','like',"%{$user_code}%");
            });
        }
        $list=$list->orderBy('status','asc')
            ->orderBy('add_time','asc')
            ->paginate($perPage, ['*'], '', $page);
        return $list;
    }


    /**
     * 暂停资质修改数据
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function suspendApply($id){

        $data =[
            'status'=>2,
            'verify_memo'=>'资质暂停导致资质审核失败',
            'update_time'=>time(),
        ];
        $suspend =app('model_lisence_apply')->where(['lisence_id'=>$id,'status'=>1])->update($data);
        if ($suspend) return $suspend;
        throw new BasicException(93122);
    }

    /**
     * 查询数据是否存在
     * @param  $id
     * @return bool
     * @throws BasicException
     */

    public  function issetData($id){
        $data =app('model_lisence_apply')->where(['lisence_id'=>$id,'status'=>1])->first();
        return $data;
    }


    /**
     * 删除资质修改数据
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public function delLisenceApply($id){

        $data =[
            'status'=>2,
            'verify_memo'=>'资质删除导致资质审核失败',
            'update_time'=>time(),
        ];
        app('model_lisence_apply')->whereIn('lisence_id',$id)->where('status',1)->update($data);
    }

    /**
     * 资质审核失败操作
     * @param  $id
     * @param  $admin_id
     * @param  $verify_memo
     * @return bool
     * @throws BasicException
     */
    public function  examineFail($id,$admin_id,$verify_memo){
        $data =[
            'status'=>2,
            'verify_memo'=>$verify_memo,
            'update_time'=>time(),
            'admin_id'=>$admin_id,
        ];
        $suspend =app('model_lisence_apply')->where('id',$id)->update($data);
        if ($suspend) return $suspend;
        throw new BasicException(93127);
    }

    /**
     * 资质审核成功操作
     * @param  $id
     * @param  $admin_id
     * @param  $verify_memo
     * @return bool
     * @throws BasicException
     */
    public function  examineSuccess($id,$admin_id,$verify_memo){
        $data =[
            'status'=>3,
            'verify_memo'=>$verify_memo,
            'update_time'=>time(),
            'admin_id'=>$admin_id,
        ];
        $suspend =app('model_lisence_apply')->where('id',$id)->update($data);
        if ($suspend) return $suspend;
        throw new BasicException(93129);
    }

    /**
     * 获取资质审核详情
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public  function  applyDetail($id){
        $detail = app('model_lisence_apply')->where('id',$id)->first();
        if ($detail)  return $detail;
        throw  new  BasicException(50018);
    }
    /**
     * 通过不显示资质审核
     * @param  $id
     * @return bool
     * @throws BasicException
     */
    public  function  saveShow($id){
        $show = app('model_lisence_apply')->where('id',$id)->update(['is_show'=>2]);
        if ($show)  return  $show;
        throw  new  BasicException(93138);
    }

    /**
     * 编辑不显示资质审核
     * @param  $param
     * @return bool
     * @throws BasicException
     */

    public  function  saveShowApply($param){
        // $saveShowApply = app('model_lisence_apply')->where('id',$param['id'])->update($param);
        $saveShowApply = app('model_lisence_apply')->where('id',$param['id'])->first();
        if(!empty($param['tag_ids'])) $saveShowApply->tag_ids = $param['tag_ids'];
        if(!empty($param['tag_image'])) $saveShowApply->tag_image = $param['tag_image'];
        $res = $saveShowApply->save();
        if ($res)  return true;
        throw  new  BasicException(93139);
    }

    /**
     * 编辑不显示资质审核数据
     * @param  $id
     * @return bool
     * @throws BasicException
     */

    public  function  saveShowApplyData($id){
        $saveShowApply = app('model_lisence_apply')->where('id',$id)->select('id','tag_ids','tag_image')->first();
        if ($saveShowApply)  return  $saveShowApply;
        throw  new  BasicException(93142);
    }


}


?>